﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class OutboundValidationDetailDA : IOutboundValidationDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DYN_PICK = "@DYN_PICK";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string RF_ASSISTED_PICK = "@RF_ASSISTED_PICK";
        private string RF_DIRECTED_PICK = "@RF_DIRECTED_PICK";
        private string SEQ_NO = "@SEQ_NO";
        private string SQL_CHECK_OUTBOUND_VALIDATION_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@OUTBOUND_VALIDATION_DETAIL WHERE VALIDATION_ID = @VALIDATION_ID AND SEQ_NO = @SEQ_NO ";
        private string SQL_DELETE_OUTBOUND_VALIDATION_DETAIL = " DELETE FROM @_@OUTBOUND_VALIDATION_DETAIL WHERE VALIDATION_ID = @VALIDATION_ID";
        private string SQL_INSERT_OUTBOUND_VALIDATION_DETAIL = " INSERT INTO @_@OUTBOUND_VALIDATION_DETAIL ( WH_ID, VALIDATION_ID, SEQ_NO, VALIDATION_ROUTINE, IS_ACTIVE, RF_DIRECTED_PICK, RF_ASSISTED_PICK, DYN_PICK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @VALIDATION_ID, @SEQ_NO, @VALIDATION_ROUTINE, @IS_ACTIVE, @RF_DIRECTED_PICK, @RF_ASSISTED_PICK, @DYN_PICK, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL = " SELECT  ROWID, WH_ID, VALIDATION_ID, SEQ_NO, VALIDATION_ROUTINE, IS_ACTIVE, RF_DIRECTED_PICK, RF_ASSISTED_PICK, DYN_PICK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@OUTBOUND_VALIDATION_DETAIL ";
        private string SQL_UPDATE_OUTBOUND_VALIDATION_DETAIL = " UPDATE @_@OUTBOUND_VALIDATION_DETAIL SET WH_ID = @WH_ID, VALIDATION_ROUTINE = @VALIDATION_ROUTINE, IS_ACTIVE = @IS_ACTIVE, RF_DIRECTED_PICK = @RF_DIRECTED_PICK, RF_ASSISTED_PICK = @RF_ASSISTED_PICK, DYN_PICK = @DYN_PICK, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE VALIDATION_ID = @VALIDATION_ID AND SEQ_NO = @SEQ_NO  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string VALIDATION_ID = "@VALIDATION_ID";
        private string VALIDATION_ROUTINE = "@VALIDATION_ROUTINE";
        private string WH_ID = "@WH_ID";

        public bool CheckOutboundValidationDetailIDUnique(string outboundValidationDetailID, string seqNo, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationDetailID), new SqlParameter("@SEQ_NO", seqNo) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_OUTBOUND_VALIDATION_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckOutboundValidationDetailIDUnique(DataBase dataBase, DbTransaction tran, string outboundValidationDetailID, string seqNo, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationDetailID), new SqlParameter("@SEQ_NO", seqNo) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_OUTBOUND_VALIDATION_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteOutboundValidationDetail(string outboundValidationDetailID, List<string> seqNO, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < seqNO.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationDetailID), new SqlParameter("@SEQ_NO", seqNO[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID) + " AND SEQ_NO = @SEQ_NO ", paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteOutboundValidationDetail(DataBase dataBase, DbTransaction tran, string outboundValidationDetailID, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationDetailID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID), paras);
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteOutboundValidationDetail(DataBase dataBase, DbTransaction tran, string outboundValidationDetailID, List<string> seqNO, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < seqNO.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationDetailID), new SqlParameter("@SEQ_NO", seqNO[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID) + " AND SEQ_NO = @SEQ_NO ", paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllOutboundValidationDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllOutboundValidationDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public OutboundValidationDetailInfo GetOutboundValidationDetailByID(string outboundvalidationdetailID, string seqNO, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID) + " WHERE VALIDATION_ID = @VALIDATION_ID AND SEQ_NO = @SEQ_NO  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundvalidationdetailID), new SqlParameter("@SEQ_NO", seqNO) };
            OutboundValidationDetailInfo outboundValidationDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    outboundValidationDetailInfo = new OutboundValidationDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VALIDATION_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["VALIDATION_ROUTINE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["RF_DIRECTED_PICK"].ToString(), reader["RF_ASSISTED_PICK"].ToString(), reader["DYN_PICK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return outboundValidationDetailInfo;
        }

        public OutboundValidationDetailInfo GetOutboundValidationDetailByID(DataBase dataBase, DbTransaction tran, string outboundvalidationdetailID, string seqNO, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID) + " WHERE VALIDATION_ID = @VALIDATION_ID AND SEQ_NO = @SEQ_NO  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundvalidationdetailID), new SqlParameter("@SEQ_NO", seqNO) };
            OutboundValidationDetailInfo outboundValidationDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                outboundValidationDetailInfo = new OutboundValidationDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VALIDATION_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["VALIDATION_ROUTINE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["RF_DIRECTED_PICK"].ToString(), reader["RF_ASSISTED_PICK"].ToString(), reader["DYN_PICK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return outboundValidationDetailInfo;
        }

        public List<OutboundValidationDetailInfo> GetOutboundValidationDetailByList(string outboundvalidationdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID) + " WHERE VALIDATION_ID = @VALIDATION_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundvalidationdetailID) };
            List<OutboundValidationDetailInfo> outboundValidationDetailList = new List<OutboundValidationDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    OutboundValidationDetailInfo outboundValidationDetailInfo = new OutboundValidationDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VALIDATION_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["VALIDATION_ROUTINE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["RF_DIRECTED_PICK"].ToString(), reader["RF_ASSISTED_PICK"].ToString(), reader["DYN_PICK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    outboundValidationDetailList.Add(outboundValidationDetailInfo);
                }
            }
            return outboundValidationDetailList;
        }

        public DataSet GetOutboundValidationDetailByQueryList(List<string> sqlWhere, OutboundValidationDetailQueryEntity outboundvalidationdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!outboundvalidationdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, outboundvalidationdetailQuery.CurrentPage, outboundvalidationdetailQuery.PageSize, outboundvalidationdetailQuery.SortField, outboundvalidationdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetOutboundValidationDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, OutboundValidationDetailQueryEntity outboundvalidationdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, outboundvalidationdetailQuery.CurrentPage, outboundvalidationdetailQuery.PageSize, outboundvalidationdetailQuery.SortField, outboundvalidationdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertOutboundValidationDetail(OutboundValidationDetailInfo outboundValidationDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                outboundValidationDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidationDetail_Parameters(outboundValidationDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertOutboundValidationDetail(DataBase dataBase, DbTransaction tran, OutboundValidationDetailInfo outboundValidationDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                outboundValidationDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidationDetail_Parameters(outboundValidationDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_OutboundValidationDetail_Parameters(OutboundValidationDetailInfo outboundValidationDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.VALIDATION_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SEQ_NO, SqlDbType.Int, 4), new SqlParameter(this.VALIDATION_ROUTINE, SqlDbType.VarChar, 50), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.RF_DIRECTED_PICK, SqlDbType.VarChar, 1), new SqlParameter(this.RF_ASSISTED_PICK, SqlDbType.VarChar, 1), new SqlParameter(this.DYN_PICK, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(outboundValidationDetailInfo.WhID))
            {
                paramArray[0].Value = outboundValidationDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = outboundValidationDetailInfo.ValidationID;
            paramArray[2].Value = outboundValidationDetailInfo.SeqNo;
            paramArray[3].Value = outboundValidationDetailInfo.ValidationRoutine;
            paramArray[4].Value = outboundValidationDetailInfo.IsActive;
            paramArray[5].Value = outboundValidationDetailInfo.RfDirectedPick;
            paramArray[6].Value = outboundValidationDetailInfo.RfAssistedPick;
            paramArray[7].Value = outboundValidationDetailInfo.DynPick;
            paramArray[8].Value = outboundValidationDetailInfo.CreatedBy;
            paramArray[9].Value = outboundValidationDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateOutboundValidationDetail(OutboundValidationDetailInfo outboundValidationDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                outboundValidationDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidationDetail_Parameters(outboundValidationDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateOutboundValidationDetail(DataBase dataBase, DbTransaction tran, OutboundValidationDetailInfo outboundValidationDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                outboundValidationDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidationDetail_Parameters(outboundValidationDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_OUTBOUND_VALIDATION_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

